package com.ksc.storekeeper.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.TabData;
import com.gwtplatform.mvp.client.TabDataBasic;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.TabInfo;
import com.gwtplatform.mvp.client.annotations.TitleFunction;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.TabContentProxyPlace;
import com.ksc.storekeeper.client.mvp.NameTokens;
import com.ksc.storekeeper.client.mvp.StoreKeeperGinInjector;
import com.ksc.storekeeper.client.presenter.AddUserPresenter.AddUserProxy;
import com.ksc.storekeeper.client.view.AddUserView;
import com.ksc.storekeeper.client.view.AddUserView.AddUserUiHandler;
import com.ksc.storekeeper.shared.dto.UserDTO;
import com.ksc.storekeeper.shared.rpc.ProfileServiceAsync;

public class AddUserPresenter extends Presenter<AddUserView, AddUserProxy> implements AddUserUiHandler {

	@Inject
	private ProfileServiceAsync profileService;

	@Inject
	public AddUserPresenter(EventBus eventBus, AddUserView view, AddUserProxy proxy) {
		super(eventBus, view, proxy);

	}

	@ProxyCodeSplit
	@NameToken(NameTokens.ADD_USER)
	public interface AddUserProxy extends TabContentProxyPlace<AddUserPresenter> {

	}

	@TabInfo(container = SettingsPresenter.class)
	static TabData getTabLabel(StoreKeeperGinInjector ginjector) {
		return new TabDataBasic(ginjector.getStoreKeeperConstants().addUser(), 0);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, SettingsPresenter.TYPE_SetTabContent, this);
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@TitleFunction
	public String getTranslatedTitle(StoreKeeperGinInjector gInjector) {
		return gInjector.getStoreKeeperConstants().addUser();
	}

	@Override
	public void addUser(UserDTO u) {
		profileService.addUser(u, new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				Window.alert("User added successfully.");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Failed :" + caught.getMessage());
			}
		});

	}
}
